package com.example.notificationservice.controller;

import com.example.notificationservice.entity.NotificationEntity;
import com.example.notificationservice.service.NotificationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: dang
 * @Description: TODO
 * @Date: 2025/4/15 18:21 星期二
 */
@RestController
@RequestMapping("/notification")
public class NotificationController {

    @Autowired
    private NotificationService notificationService;

    // 创建通知
    @PostMapping
    public ResponseEntity<Map<String, Object>> createNotification(@RequestBody NotificationEntity notification) {
        NotificationEntity createdNotification = notificationService.createNotification(notification);
        return ResponseEntity.ok(wrapResponse(createdNotification));
    }

    // 根据通知 ID 获取通知
    @GetMapping("/{id}")
    public ResponseEntity<Map<String, Object>> getNotificationById(@PathVariable String id) {
        NotificationEntity notification = notificationService.getNotificationById(id);
        if (notification != null) {
            return ResponseEntity.ok(wrapResponse(notification));
        } else {
            return ResponseEntity.notFound().build();
        }
    }

    // 按 customerId 查询通知
    @GetMapping("/customer/{customerId}")
    public ResponseEntity<Map<String, Object>> getNotificationsByCustomerId(@PathVariable String customerId) {
        List<NotificationEntity> notifications = notificationService.getNotificationsByCustomerId(customerId);
        return ResponseEntity.ok(wrapResponse(notifications));
    }

    // 按类型查询通知
    @GetMapping("/type/{type}")
    public ResponseEntity<Map<String, Object>> getNotificationsByType(@PathVariable String type) {
        List<NotificationEntity> notifications = notificationService.getNotificationsByType(type);
        return ResponseEntity.ok(wrapResponse(notifications));
    }

    // 按 customerId 和类型查询通知
    @GetMapping("/customer/{customerId}/type/{type}")
    public ResponseEntity<Map<String, Object>> getNotificationsByClientIdAndType(
            @PathVariable String customerId,
            @PathVariable String type) {
        List<NotificationEntity> notifications = notificationService.getNotificationsByCustomerIdAndType(customerId, type);
        return ResponseEntity.ok(wrapResponse(notifications));
    }

    // 获取未读通知
    @GetMapping("/customer/{customerId}/unread")
    public ResponseEntity<Map<String, Object>> getUnreadNotifications(@PathVariable String customerId) {
        List<NotificationEntity> notifications = notificationService.getUnreadNotificationsByCustomerId(customerId);
        return ResponseEntity.ok(wrapResponse(notifications));
    }

    // 更新通知状态为已读
    @PatchMapping("/{id}/read")
    public ResponseEntity<Map<String, Object>> markNotificationAsRead(@PathVariable String id) {
        NotificationEntity notification = notificationService.markNotificationAsRead(id);
        if (notification != null) {
            return ResponseEntity.ok(wrapResponse(notification));
        } else {
            return ResponseEntity.notFound().build();
        }
    }

    // 删除通知
    @DeleteMapping("/{id}")
    public ResponseEntity<Void> deleteNotification(@PathVariable String id) {
        notificationService.deleteNotification(id);
        return ResponseEntity.noContent().build();
    }

    // 获取所有通知
    @GetMapping
    public ResponseEntity<Map<String, Object>> getAllNotifications() {
        List<NotificationEntity> notifications = notificationService.getAllNotifications();
        return ResponseEntity.ok(wrapResponse(notifications));
    }

    // 辅助方法：包装响应数据
    private Map<String, Object> wrapResponse(Object data) {
        Map<String, Object> response = new HashMap<>();
        response.put("status", "OK");
        response.put("data", data);
        return response;
    }
}
